// closeDialog()
//不在房间的次数
var noRoomCount = 0;
//广告词
var searchWord;
function start (p) {
    searchWord = p.searchWord
    setConsole()
    main()
}

function main () {
    tool.log("开始")
    noRoomCount = 0
    launchApp("TT语音");
    let name = getRoomName()
    if (name !== null) {
        tool.log("已经在房间")
        back()
    }
    id("item_sdv_user").findOne(20000)
    enterRoom()
}

//进入房间发送消息
function enterRoom () {
    if (clickId("item_sdv_user")) {
        sleep(500)
        let name = getRoomName()
        tool.log(`进入${name}`)
        if (name !== null) {
            if (clickId("v_input_bg")) {
                tool.log("开始输入")
                sleep(100)
                setText(searchWord)
                if (text("发送").exists()) {
                    sleep(100)
                    click("发送")
                    tool.log("发送成功")
                    sleep(200)
                    refreshRoom()
                } else {
                    tool.log("发送失败")
                    refreshRoom()
                }
            } else {
                tool.log("输入失败")
                refreshRoom()
            }
        } else {
            tool.log("进入房间失败")
            refreshRoom()
        }
    } else {
        tool.log("没有房间")
        noRoomCount++
        if (noRoomCount > 10) {
            tool.log("重启app")
            killApp("TT语音")
            main()
        }
        refreshRoom()
    }
}

//刷新房间
function refreshRoom () {
    closeDialog()
    let name = getRoomName()
    if (name !== null) {
        back()
    }
    id("item_sdv_user").findOne(5000)
    swipe(500, 800, 550, 1800, 200)
    sleep(1000)
    enterRoom()
}

//点击id
function clickId (ids) {
    id(ids).findOne(5000)
    if (id(ids).exists()) {
        let p = id(ids).findOne(1000).bounds()
        press(p.centerX() + 10, p.centerY(), 100);
        return true
    } else {
        return false
    }
}

//设置控制台
function setConsole () {
    console.setPosition(device.width / 2, 300)
    console.setSize(300, 100)
    console.setMaxLines(500);
    console.setTitle("控制台");
    console.show(true)
}

//当前房间名称
function getRoomName () {
    let name = id("headerChannelName").findOne(3000)
    if (name !== null) {
        name = name.text()
        //tool.log("当前房间名称" + name)
        return name;
    } else {
        //tool.log("没有找到当前房间名称")
        return null
    }
}

//关闭弹窗
function closeDialog () {
    //关键字
    let keyword = ["关闭", "下麦", "继续", "我知道了", "忽略"];
    for (let i = 0; i < keyword.length; i++) {
        if (text(keyword[i]).exists()) {
            let p = text(keyword[i]).findOne(500).bounds()
            press(p.centerX() + 10, p.centerY(), 100);
            tool.log(`关闭弹窗:${keyword[i]}`)
            sleep(500)
            return
        }
    }

    let closeIds = ['close_view', 'channelVoteClose']
    for (let i = 0; i < closeIds.length; i++) {
        if (id(closeIds[i]).exists()) {
            id(closeIds[i]).findOne(500).click()
            tool.log(`关闭弹窗:${closeIds[i]}`)
            sleep(500)
            return
        }
    }

    if(id("friendship_status_text").exists() && text("回粉").exists() && text("粉丝").exists()){
        className("android.widget.ImageButton").findOne(500).click()
    }
}

//关闭app
function killApp (appName) {
    let forcedStopStr = ["确定", "结束", "停", "强行停止"];
    let packageName = app.getPackageName(appName);//获取应用包名 通过软件名
    if (packageName) {
        app.openAppSetting(packageName);//进入应用设置信息
        sleep(2000);
        if (textContains("停止").exists()) {
            let forcedStop = textContains("停止").findOne(2000).bounds();
            click(forcedStop.centerX(), forcedStop.centerY());
            tool.log("停止app")
            sleep(1000);
        }
        for (let i = 0; i < forcedStopStr.length; i++) {
            if (text(forcedStopStr[i]).exists()) {
                let forcedStop = text(forcedStopStr[i]).findOne(2000).bounds();
                click(forcedStop.centerX(), forcedStop.centerY());
                tool.log("确定停止")
                sleep(1000);
            }
        }
        home();
    }
}
exports.main = start;
